package cn.cytong.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * User: TangGaowei
 * Date: 12-12-12
 * Time: 下午10:43
 * To change this template use File | Settings | File Templates.
 */
public class RegexTool {
    public static final String ALL_REGEX = "[\\w\\W]+?";

    /**
     * 不区分大小写的替换
     * @param str
     * @param regex
     * @param replacement
     * @return
     */
    public static String replaceAll(String str, String regex, String replacement){
        if(str != null){
            Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
            Matcher matcher = pattern.matcher(str);

            return matcher.replaceAll(replacement);
        }
        else{
            return "";
        }
    }

    /**
     * 取签单规则数据
     * @param begin
     * @param end
     * @return
     */
    public static String getRegexString(String begin, String end){
        if(begin == null && end != null){
            return "[\\w\\W]+?(?=" + end + ")";
        }
        else if(begin != null && end == null){
            return "(?<=" + begin + ")[\\w\\W]+";
        }
        else if(begin != null && end != null){
            return "(?<=" + begin + ")[\\w\\W]+?(?=" + end + ")";
        }
        else{
            return "[\\w\\W]+?";
        }
    }

    /**
     * 错误接口名称的保留
     * @param begin
     * @param end
     * @return
     */
    public static String geRegexString(String begin, String end){
        return getRegexString(begin, end);
    }

    public static boolean check(String str, String regex){
        boolean result = false;

        if(str != null){
            Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
            Matcher matcher = pattern.matcher(str);

            if(matcher.find()) {
                String m = matcher.group(0);
                if(m.equals(str)){
                    result = true;
                }
            }
        }
        return result;
    }

    public static String value(String str, String regex){
        if(str != null){
            Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
            Matcher matcher = pattern.matcher(str);

            if(matcher.find()) {
                String m = matcher.group(0);
                return m;
            }
        }
        return null;
    }

    public static Integer max(String str, String regex){
        Integer retVal = null;
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(str);
        boolean result = matcher.find();
        while(result) {
            String m = matcher.group(0);
            //System.out.println(m);
            if(StringTool.isNumeric(m)){
                Integer number = Integer.valueOf(m);
                if(retVal == null){
                    retVal = number;
                }
                else if(number > retVal){
                    retVal = number;
                }
            }
            result = matcher.find();
        }

        return retVal;
    }

    public static List<String> values(String str, String regex){
        List<String> retVal = new ArrayList<String>();
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(str);
        boolean result = matcher.find();
        while(result) {
            String m = matcher.group(0);
            retVal.add(m);
            result = matcher.find();
        }

        return retVal;
    }
}
